package com.chpc.edeal.controller.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.chpc.edeal.controller.CategoryController;
import com.chpc.edeal.services.CategoryHandler;
import com.chpc.edeal.vo.Category;

@Controller
@RequestMapping(value="/admin")
public class CategoryControllerImpl implements CategoryController {
	
	@Autowired
	private  CategoryHandler categoryHandler;
	

	public CategoryHandler getCategoryHandler() {
		return categoryHandler;
	}
	public void setCategoryHandler(CategoryHandler categoryHandler) {
		this.categoryHandler = categoryHandler;
	}
	@ModelAttribute("Category")
	public Category populateCategory() {
		// TODO Auto-generated method stub
		Category c=new Category();
		c.setName("add caegory");
		
		return c;
	}
	@RequestMapping(value="/ShowCategories.htm",method=RequestMethod.GET)
	public String getCategoryForm() {
		// TODO Auto-generated method stub
		return "admin/ShowCategories";
	}
	@RequestMapping(value="/AddCategory.htm",method=RequestMethod.POST)
	public void AddCategory(@ModelAttribute("Category")Category category,HttpServletResponse res) {
	
		categoryHandler.addCategory(category);
		// TODO Auto-generated method stub
		/*res.setContentType("image/jpg");
		PrintWriter pw=null;
		try {
			pw=res.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}
		InputStream is=null;
		
		try {
			is = category.getImage().getInputStream();
			System.out.println(is);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			System.out.println(e1);
		}
		int c=0;
		try {
			for(;(c=is.read())!=-1;){
				System.out.write(c);
			pw.write(c);
			
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
		
	}
	
	public List<Category> populateCategorys() {
		// TODO Auto-generated method stub
		return null;
	}
	@RequestMapping(value="/display.htm",method=RequestMethod.GET)
	public String displayImage(HttpServletResponse res,Writer out) {
		try {
			out.append("wfsf");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*Category c;
		res.setContentType("image/jpg");
		PrintWriter pw=null;
		try {
			pw=res.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		InputStream is=null;
		try {
			is = category.getImage().getInputStream();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		byte[] b = null;
		try {
			for(;is.read(b)>0;){
			pw.print(b.toString());
			
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		*/
		// TODO Auto-generated method stub
		return null;
	}
	

}
